﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using Super.Core.DBFactory;
using Super.Core.IDBO;

namespace SuperCoder
{
    public partial class frmConnectDB : Form
    {
        public List<string> listOfTables;
        public List<string> listOfViews;
        public frmConnectDB()
        {
            InitializeComponent();

            //设置默认设置；
            for (int i = 0; i < this.ddlDBType.Items.Count; ++i)
            {
                if (ddlDBType.Items[i].ToString() == Config.DB_TYPE)
                {
                    this.ddlDBType.SelectedIndex = i;
                    break;
                }
            }
            this.txtDBServer.Text = Config.DB_SERVER;
            this.txtDBName.Text = Config.DB_NAME;
            this.txtUserName.Text = Config.DB_USERNAME;
            this.txtPassWord.Text = Config.DB_PASSWORD;
           

        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            btnConnect.Enabled = false;
            string dbServer = txtDBServer.Text.Trim();
            string userName = txtUserName.Text.Trim();
            string passWord = txtPassWord.Text.Trim();
            string dbName = txtDBName.Text.Trim();
            string dbType = GetSelVer();
            if (dbServer == "" || userName == "" || passWord == "" || dbName == "")
            {
                MessageBox.Show("提示：数据库服务器地址或用户名，密码，数据库名称不能为空！");
                btnConnect.Enabled = true;
                return;
            }

            //测试数据库连接是否有效；
            string connStr = "";
            if (ddlDBType.SelectedItem.ToString() == "SQLServer 2005" || ddlDBType.SelectedItem.ToString() == "SQLServer 2000")
            {
                connStr = "user id=" + userName + ";password=" + passWord + ";initial catalog=" + dbName + ";data source=" + dbServer;
            }
            //有效的化 则获取所有的数据库表和视图；
            SqlConnection myCn = new SqlConnection(connStr);
            try
            {
                this.Text = "正在连接服务器，请稍候...";
                myCn.Open();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show( "连接服务器失败！请检查服务器地址或用户名密码是否正确！");
                btnConnect.Enabled = true;
                return;
            }
            finally
            {
                myCn.Close();
            }
            Config.DB_SERVER = dbServer;
            Config.DB_TYPE = ddlDBType.SelectedItem.ToString();
            Config.DB_NAME = dbName;
            Config.DB_USERNAME = userName;
            Config.DB_PASSWORD = passWord;

            //获取下当前数据库的所有表及视图；
            IDbObject idb = DBFactory.CreateDbObj(dbType);
            idb.DbConnectStr = connStr;
            listOfTables = idb.GetTables();
            listOfViews = idb.GetVIEWs();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;

           
           



        }
      
        //得到选择的版本
        public string GetSelVer()
        {
            string dbtype = "SQL2005";
            switch (ddlDBType.Text)
            {
                case "SQL Server2000":
                    dbtype = "SQL2000";
                    break;
                case "SQL Server2005":
                    dbtype = "SQL2005";
                    break;
                default:
                    dbtype = "SQL2005";
                    break;
            }
            return dbtype;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
